package com.csjy.migration.hnnd.business;

import com.csjy.migration.hnnd.*;
import com.csjy.migration.hnnd.annotation.BusinessDataConverter;
import com.csjy.migration.hnnd.dict.AreaCode;
import com.csjy.migration.hnnd.dict.SysOrgCode;

import javax.script.ScriptException;
import java.io.FileWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static com.csjy.migration.hnnd.BusinessTool.getRigOrWro;

/**
 * 客户表
 */
@BusinessDataConverter(
        auto = false,

        insertSql = "insert into nd_customer_info (ID, CREATE_NAME, CREATE_BY, CREATE_DATE, UPDATE_NAME, UPDATE_BY, UPDATE_DATE, SYS_ORG_CODE, SYS_COMPANY_CODE, BPM_STATUS, CUSTOMER_NAME, AREA, ADRESS, COPANY_DEPART, BUSINESS, IS_BLACKLIST, BLACKLIST_MSG, CUSTOMER_TYPE, CUSTOMER_SOURCE, DELETE_FLAG, CUSTOMER_SUBJECT_NATURE, IDCARD, ORGNO, PROJECTCOUNT, PRJ_NAME_FULL_PINYIN, PRJ_NAME_SIMPLE_PINYIN, BLACKLIST_REASON, BLACKLIST_RELIEVE, CUSTOMER_SOURCE_BANK)" +
                "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",

        updateSql = "UPDATE ND_CUSTOMER_INFO\n" +
                "    SET CREATE_NAME=?, CREATE_BY=?, CREATE_DATE=?, UPDATE_NAME=?, UPDATE_BY=?, UPDATE_DATE=?, SYS_ORG_CODE=?, SYS_COMPANY_CODE=?, BPM_STATUS=?, CUSTOMER_NAME=?, AREA=?, ADRESS=?, COPANY_DEPART=?, BUSINESS=?, IS_BLACKLIST=?, BLACKLIST_MSG=?, CUSTOMER_TYPE=?, CUSTOMER_SOURCE=?, DELETE_FLAG=?, CUSTOMER_SUBJECT_NATURE=?, IDCARD=?, ORGNO=?, PROJECTCOUNT=?, PRJ_NAME_FULL_PINYIN=?, PRJ_NAME_SIMPLE_PINYIN=?, BLACKLIST_REASON=?, BLACKLIST_RELIEVE=?, CUSTOMER_SOURCE_BANK=?\n" +
                "    WHERE ID =?",


        findMigratedIdListSql = "SELECT ID FROM nd_customer_info"
)
public class NdCustomerInfo extends AbstractBusiness {

    public NdCustomerInfo(DbUtil sourceDbUtil, DbUtil targetDbUtil, SourceBusinessData sourceData, ProgramArgument programArgument, String projectId) throws SQLException {
        super(sourceDbUtil, targetDbUtil, sourceData, programArgument, projectId);
    }

    @Override
    public void findMigratedIdList() throws SQLException {
        this.migratedIdList = targetDbUtil.selectSingleColumn(this.findMigratedIdListSql, new ArrayList());
    }

    @Override
    public void migrate() throws Exception {
        NdEnterpriseInfo ndEnterpriseInfo = new NdEnterpriseInfo(sourceDbUtil,targetDbUtil,sourceData,programArgument,projectId);
        NdPersonInfo ndPersonInfo = new NdPersonInfo(sourceDbUtil,targetDbUtil,sourceData,programArgument,projectId);

        List<Map> btEnterpriseBasicList = sourceDbUtil.selectByParams("\n" +
            "SELECT beb.*,\n" +
            "(\n" +
            "\tSELECT max(bib.clientkind) FROM BT_ITEM_BASIC bib WHERE bib.ENTERPRISENO = beb.ENTERPRISENO \n" +
            ") clientkind,\n" +
            "(\n" +
            "\tSELECT max(bib.MANAGERA) FROM BT_ITEM_BASIC bib WHERE bib.ENTERPRISENO = beb.ENTERPRISENO \n" +
            ") MANAGERA,\n" +
            "(\n" +
            "\tSELECT max(bib.ORGCODE) FROM BT_ITEM_BASIC bib WHERE bib.ENTERPRISENO = beb.ENTERPRISENO \n" +
            ") ORGCODE\n" +
            "FROM BT_ENTERPRISE_BASIC beb \n" +
            "WHERE beb.ASSURECODE = 9051\n", new ArrayList());

        System.out.println(btEnterpriseBasicList.size());
        for(Map row : btEnterpriseBasicList) {

            try{
                if(BusinessTool.getRigOrWro(row.get("PROCHARACTER"))){
                    ndPersonInfo.customMigrate(row);
                }else{
                    ndEnterpriseInfo.customMigrate(row);
                }
                int needMigratedResult = this.needMigrated(row.get("ENTERPRISENO").toString());

                if(needMigratedResult == 0) {
                    continue;
                }

                if(needMigratedResult == 1) {
                    this.targetDbUtil.updateByParams(updateSql,this.buildValues(row),true);
//                this.batchUpdateValues.add(this.buildValues(row));
                } else {
                    this.targetDbUtil.updateByParams(insertSql,this.buildValues(row),false);
//                this.batchInsertValues.add(this.buildValues(row));
                }

            } catch (Exception e) {
            throw new Exception(e);
        }


        }
        ndEnterpriseInfo.start();
        ndPersonInfo.start();

    }
    public List buildValues(Map row) throws Exception {
        List values = new ArrayList();
        //ID	主键
        values.add(row.get("ENTERPRISENO").toString());
        //CREATE_NAME	创建人名称
        values.add(BusinessTool.getEmpNameByEmpCode(row.get("MANAGERA")));
        //CREATE_BY	创建人登录名称
        values.add(row.get("MANAGERA"));
        //CREATE_DATE	创建日期
        values.add(row.get("CREATE_TIME"));
        //UPDATE_NAME	更新人名称
        values.add(null);
        //UPDATE_BY	更新人登录名称
        values.add(null);
        //UPDATE_DATE	更新日期
        values.add(row.get("MODIFY_TIME"));
        //SYS_ORG_CODE	所属部门
        values.add(DictTool.getTargetDictCode(SysOrgCode.class, row.get("ORGCODE")));
        //SYS_COMPANY_CODE	所属公司
        values.add("002");
        //BPM_STATUS	流程状态
        values.add(1);
        //CUSTOMER_NAME	客户名称
        values.add(row.get("PRONAME"));
        // AREA	所属区域 字典 clientArea
        values.add(row.get("CLIENTAREA") == null|| row.get("CLIENTAREA").equals("") ? "湖南省" : DictTool.getTargetDictCode(AreaCode.class,row.get("CLIENTAREA")));
        // ADRESS	注册地址
        values.add(row.get("LOGADDRESS"));
        // COPANY_DEPART	性质
        Object copanydepart = null;
        if (getRigOrWro(row.get("PROCHARACTER"))) {
            copanydepart = 6;
        }else{
            copanydepart = Tool.covertToBigDecimal(row.get("PROCHARACTER"));
        }
        values.add(copanydepart);
        //BUSINESS	业务
        values.add(Tool.decodeUnicode(row.get("MOSTLY_DEALAREA")));
        //IS_BLACKLIST	黑名单
        values.add(null);
        //BLACKLIST_MSG	黑名单说明
        values.add(null);
        //CUSTOMER_TYPE	客户类型
        values.add(null);
        //CUSTOMER_SOURCE	客户来源
        values.add(null);
        //DELETE_FLAG	删除标志
        values.add(0);
        //CUSTOMER_SUBJECT_NATURE	客户主体性质
        String nature = null;
        if (getRigOrWro(row.get("PROCHARACTER"))) {
            nature = "自然人";
        } else {
            nature = "法人";
        }
        values.add(nature);
        // IDCARD	身份证号码  101>自然人 其他数字>法人
        String idcard = null;
        if (getRigOrWro(row.get("PROCHARACTER"))) {
            idcard = row.get("BUSIREGISTCODE").toString();
        }
        values.add(idcard);
        // ORGNO	组织机构代码证编号
        String orgno = null;
        if (!getRigOrWro(row.get("PROCHARACTER"))) {
            orgno = row.get("REGORGCODE").toString();
        }
        values.add(orgno);
        // 项目个数
        values.add(projectCount(row.get("ENTERPRISENO")));
        //TODO PRJ_NAME_FULL_PINYIN	客户名全拼音
        values.add(null);
        //TODO PRJ_NAME_SIMPLE_PINYIN	客户名简拼音
        values.add(null);
        //BLACKLIST_REASON	设置黑名单原因
        values.add(null);
        //BLACKLIST_RELIEVE	黑名单解除日期
        values.add(null);
        //CUSTOMER_SOURCE_BANK	客户来源_是否银行
        values.add(null);
        return values;
    }


    public Integer  projectCount(Object paraMeter) throws SQLException {
        String jdbcSql = "SELECT COUNT(*) as COUNTS FROM BT_ITEM_BASIC WHERE ENTERPRISENO = ?";
        List<Map> projectCount = sourceDbUtil.selectByParams(jdbcSql, Arrays.asList(new String[]{paraMeter.toString()}));
        if(projectCount == null || projectCount.equals("")){
            return 0;
        }
        return Integer.parseInt(projectCount.get(0).get("COUNTS").toString());
    }
}


    